package gcontent.channel.http;

import gcontent.utils.DateUtil;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;

public class CookieEntry {
	
	private String path;
	private String domain;
	private String expires;
	
	private HashMap<String, String> name_value = new HashMap<String, String>();
	
	public String getPath() { return this.path; }
	public void setPath(String path) { this.path = path; }
	
	public String getDomain() { return this.domain; }
	public void setDomain(String domain) { this.domain = domain; }
	
	public String getExpires() { return this.expires; }
	public void setExpires(String expires) { this.expires = expires; }
	
	public boolean inDomain(String url_domain)
	{
		if(this.domain.charAt(0) == '.') {
			return url_domain.contains(this.domain);
		} 			
		
		return url_domain.equals(this.domain);		
	}
	
	public boolean inPath(String url_path)
	{
		return url_path.startsWith(this.path);		
	}
	
	public boolean isExpired()
	{
		Calendar cal;
		Date date_cookie;
				
		if(this.expires == null)
			return false;
		
		cal = Calendar.getInstance();
		date_cookie = DateUtil.parseDate(this.expires);

		return cal.getTime().compareTo(date_cookie) >= 0;								
	}
	
	public void setValue(String name, String value)
	{
		this.name_value.put(name, value);
	}
	
	public void getValues(HashMap<String, String> map)
	{
		for(Iterator<String> it = name_value.keySet().iterator(); it.hasNext(); ) {
			String name, value;
			
			name = it.next();
			value = name_value.get(name);
			
			map.put(name, value);				
		}
	}	

}
